package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.framework.util.DateUtils;
import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.TbCloseAccounts;
import com.pignest.hotel.definition.PayMethod;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class CloseAccVbox extends Vbox implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5884449322537604605L;
    private Textbox qRoomName;
    private Listbox qRoomType;
    private Textbox qUserName;
    private Listbox paymentMethod;
    private Listbox invoiceListbox;
    private Datebox qSDate;
    private Datebox qEDate;
    private Textbox qOrderNo;
    
    private Paging pagCloseAcc;
    private Listbox closeAccList;
	@Override
	public void onEvent(Event arg0) throws Exception {
		// TODO Auto-generated method stub

	}
	public void onCreate() {
		qRoomName =(Textbox)getFellow("qRoomName");
		qRoomType=(Listbox)getFellow("qRoomType");
		qUserName=(Textbox)getFellow("qUserName");
		paymentMethod=(Listbox)getFellow("paymentMethod");
		invoiceListbox=(Listbox)getFellow("invoiceListbox");
		qSDate =(Datebox)getFellow("qSDate");
		qEDate=(Datebox)getFellow("qEDate");
		qOrderNo=(Textbox)getFellow("qOrderNo");
		pagCloseAcc =(Paging)getFellow("pagCloseAcc");
		closeAccList=(Listbox)getFellow("closeAccList");
		final int PAGE_SIZE = pagCloseAcc.getPageSize();
		pagCloseAcc.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	query(start,PAGE_SIZE);
		      	}
		    	});
			query(0,PAGE_SIZE);
	}
  public void query(int start,int pageSize){
	  closeAccList.getItems().clear();
	  String sRoomName =qRoomName.getValue();
	  String sRoomTypeId =(String)UIUtils.getSelectedValue(qRoomType);
	  String sUserName =qUserName.getValue();
	  String sPayMethod=UIUtils.getSelectedValue(paymentMethod);
	  String sInvoice =UIUtils.getSelectedValue(invoiceListbox);
	  String sOrderNo =qOrderNo.getValue();
	  Integer iInvoice =null;
	  if(sInvoice!=null&&!sInvoice.equals("")){
		  iInvoice =Integer.valueOf(sInvoice);
	  }
	  Date sDate =qSDate.getValue();
	  Date eDate =qEDate.getValue();
	  int Total =Services.getIncomeService().queryCloseAccNoPage(sUserName, sPayMethod, sOrderNo, 
			  sRoomName, sRoomTypeId, iInvoice, sDate, eDate).size();
	  pagCloseAcc.setTotalSize(Total);
	  List<TbCloseAccounts> cAccList =Services.getIncomeService().queryCloseAccPage(sUserName, sPayMethod, sOrderNo, 
			  sRoomName, sRoomTypeId, iInvoice, sDate, eDate, start, pageSize);
	  if(cAccList==null)
		  return ;
	  int index=1;
	  for(TbCloseAccounts c:cAccList)
	  {
		  Listitem listitem = new Listitem();
		  listitem.setValue(c.getLiving());
		  listitem.setParent(closeAccList);
		  listitem.appendChild(new Listcell(String.valueOf(index)));
		  if(c.getLiving()!=null){
		    listitem.appendChild(new Listcell(c.getLiving().getRoom().getName()));
		    listitem.appendChild(new Listcell(c.getLiving().getRoom().getType().getName()));
		  }else{
			  listitem.appendChild(new Listcell("")); 
			  listitem.appendChild(new Listcell("")); 
		  }
		  listitem.appendChild(new Listcell(Utils.toPrice(c.getAccounts())));
		  listitem.appendChild(new Listcell(Utils.toPrice(c.getNeedAccounts())));
		  listitem.appendChild(new Listcell(PayMethod.valueOf(c.getType()).getDisplayName()));
		  listitem.appendChild(new Listcell(c.getInvoice2()));
		  listitem.appendChild(new Listcell(c.getUsername()));
		  listitem.appendChild(new Listcell(DateUtil.toString(c.getTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
		  listitem.appendChild(new Listcell(c.getOrderNo()));
		  if(c.getLiving()!=null){
		    listitem.appendChild(new Listcell(Utils.toPrice(c.getLiving().getRoomPrice())));
		  }else
		  {
			  listitem.appendChild(new Listcell("")); 
		  }
		  index++;
	  }
  }
}
